#!/bin/bash

# Function to display help information
show_help() {
    echo "Usage: $0 [options]"
    echo
    echo "Options:"
    echo "  -h, --help    Show this help message and exit"
    echo "  -o, --setos   set os"
    echo "  -f, --file    handle pkg file copy delete"
    echo "  -a, --ca      create all ca file"
    echo "  -e, --etcd    install etcd"
    echo "  -p, --haproxy install haproxy and keepalive"
    echo "  -m, --master  install k8s master node"
    echo "  -d, --docker  install docker"
    echo "  -l, --load    load docker image to harbor"
    echo "  -n, --node    install k8s worker node"
    echo "  -c, --calico  install calico and coredns"
    echo "  -w, --whole   install k8s master cluster all"
}

set_os() {
  ansible-playbook ../playbooks/set-os.yml
}

inst_etcd() {
  ansible-playbook ../playbooks/etcd.yml
}

inst_haproxy_keepalive() {
  ansible-playbook ../playbooks/haproxy-keepalive.yml
}

handle_pkg_f() {
  python ../pkg/copy.py $1
  r=$?
  if [ $r -eq 0 ]; then
    if [[ "$1" == "copy" ]]; then 
      ansible-playbook ../playbooks/copy-file.yml --tags=copy
    elif [[ "$1" == "delete" ]]; then
      ansible-playbook ../playbooks/copy-file.yml --tags=delete
    fi
  fi
}

create_ca() {
  ansible-playbook ../playbooks/create_ca.yml
}

inst_master_node() {
  ansible-playbook ../playbooks/k8s-master.yml
}

inst_docker() {
  ansible-playbook ../playbooks/docker.yml
}

load_image() {
  cd ../pkg
  sh push_images_to_harbor.sh
}

inst_worker_node() {
  ansible-playbook ../playbooks/k8s-node.yml -e 'k8shosts=k8snode' --tags=node
  python get_ansible_host_group_msg.py k8snode a > /tmp/host-list
  ansible-playbook ../playbooks/k8s-node.yml -e 'k8shosts=k8smaster' --tags=master
}

inst_calico_coredns() {
  master_top1_ip=`python3 get_ansible_host_group_msg.py k8smaster | awk 'NR==1{print}'`
  ansible-playbook ../playbooks/calico-coredns.yml -e hostsgroup=$master_top1_ip
}

# Check if no arguments were provided
if [ $# -eq 0 ]; then
    show_help
    exit 1
fi

# Process the arguments
while [[ $# -gt 0 ]]; do
    case $1 in
        -h|--help)
            show_help
            exit 0
            ;;
        -o|--setos)
            set_os
            echo "set-os succeed"
	    exit 0
            ;;
        -a|--ca)
            create_ca
            echo "create_ca succeed"
	    exit 0
	    ;;
        -e|--etcd)
	    inst_etcd
            echo "install etcd succeed"
	    exit 0
	    ;;
        -p|--haproxy)
	    inst_haproxy_keepalive
            echo "install haproxy keepalive"
	    exit 0
	    ;;
        -m|--master)
	    inst_master_node
            echo "install k8s master node"
	    exit 0
	    ;;
        -d|--docker)
	    inst_docker
            echo "install docker"
	    exit 0
	    ;;
        -l|--load)
	    load_image
            echo "load image"
	    exit 0
	    ;;
        -n|--node)
	    inst_worker_node
            echo "install worder node"
	    exit 0
	    ;;
        -c|--calico)
	    inst_calico_coredns
            echo "install calico and coredns"
	    exit 0
	    ;;
        -w|--whole)
            set_os
	    handle_pkg_f copy
            create_ca
	    inst_etcd
	    inst_haproxy_keepalive
	    inst_master_node
            echo "install k8s cluster all"
	    exit 0
	    ;;
        -f|--file)
            if [ -n "$2" ]; then
	        handle_pkg_f $2
                shift # Shift past the argument
            else
                echo "Error: -o option requires an argument."
                exit 1
            fi
            echo "handle_pkg succeed"
	    exit 0
            ;;
        *)
            echo "Unknown option: $1"
            show_help
            exit 1
            ;;
    esac
    shift # Shift to the next argument
done
